<?php
/**
 * +----------------------------------------------------------------------
 * | 九月科技-ztuc.cn
 * +----------------------------------------------------------------------
 *                      .::::.
 *                    .::::::::.            | AUTHOR: siyu
 *                    :::::::::::           | EMAIL: ztucke@ztuc.cn
 *                 ..:::::::::::'           | DATETIME: 2020/01/31
 *             '::::::::::::'
 *                .::::::::::
 *           '::::::::::::::..
 *                ..::::::::::::.
 *              ``::::::::::::::::
 *               ::::``:::::::::'        .:::.
 *              ::::'   ':::::'       .::::::::.
 *            .::::'      ::::     .:::::::'::::.
 *           .:::'       :::::  .:::::::::' ':::::.
 *          .::'        :::::.:::::::::'      ':::::.
 *         .::'         ::::::::::::::'         ``::::.
 *     ...:::           ::::::::::::'              ``::.
 *   ```` ':.          ':::::::::'                  ::::..
 *                      '.:::::'                    ':'````..
 * +----------------------------------------------------------------------
 */
/**
 * Created by PhpStorm.
 * AdminLog: Administrator
 * Date: 2020/3/14
 * Time: 17:18
 */

namespace app\lnadmin\controller;
use app\core\InitController;
use think\facade\View;
use think\facade\Db;
use app\lnadmin\model\admin\AdminLog as AdminLogModel;
class AdminLog extends InitController
{

    // 列表
    public function list()
    {
        return View::fetch('adminlog');
    }
 /**
     * 操作日志JSON
     * @param string $limit 分页
     * @throws \think\db\exception\DbException
     */
    public function index_json($limit='15')
    {
        $list = AdminLogModel::order('create_time desc')->paginate($limit);

        if (!empty($list)) {
            // 获取数据
            foreach ($list as $key => $val) {
                 $list[$key]['type'] = get_config('log.get_action')[$val['type']];
                 $list[$key]['time'] = time_trans($val['create_time']);
               }
             }
        $this->result($list);
    }

    //获取系统操作日志
    public function get_log_list()
    { 
        $list = Db::name('AdminLog')
            ->field('id,content,create_time')
            ->order('create_time desc')
            ->limit(10)
            ->select()->toArray();
        
        foreach ($list as $key => $val) {
            $list[$key]['times'] = time_trans(date('Y-m-d H:i', $val['create_time']));
        }
        $res['data'] = $list;
        return table_assign(0, '', $res);
    }

    /**
     * 删除日志
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            AdminLogModel::destroy($param['id']);
            add_admin_log('del', '删除了日志');
            $this->success('删除成功');
        }
    }
    //删除一个月前的操作日志
    public function deletelog()
    {
        AdminLogModel::where('create_time', '<= time', time() - (86400 * 30))->delete();
        add_admin_log('del', '后台行为日志');// 记录后台行为日志
        $this->success('', '', "删除日志成功！");
    }
}